//
//  XjhzVC.swift
//  Auscultation
//
//  Created by 来 on 2020/12/8.
//

import UIKit

class XjhzVC: BaseVC {
    
    var scrollView: UIScrollView!
    
    var tx: LGFLoginImageTitleView!
    var tx_i: UIImageView!
    var xm: LGFLoginImageTitleView!
    var xb: LGFLoginImageTitleView!
    var csrq: LGFLoginImageTitleView!
    var jhr: LGFLoginImageTitleView!
    var sjh: LGFLoginImageTitleView!
    var sfz: LGFLoginImageTitleView!
    var dq: LGFLoginImageTitleView!
    var xxdz: LGFLoginImageTitleView!

    override func viewDidLoad() {
        super.viewDidLoad()

        initUI()
    }
    
    // 新建患者
    @objc func tijiao(_ sender: UIButton) {
        if xm.textField.text!.lgf_IsBlank() {
            SVProgressHUD.showError("请填写姓名")
            return
        }
        if xb.textField.text!.lgf_IsBlank() {
            SVProgressHUD.showError("请选择性别")
            return
        }
        if jhr.textField.text!.lgf_IsBlank() {
            SVProgressHUD.showError("请填写监护人")
            return
        }
        if sjh.textField.text!.lgf_IsBlank() {
            SVProgressHUD.showError("请填写手机号")
            return
        }
        if !sjh.textField.text!.lgf_NSPredicate("^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9]))\\d{8}$") && !sjh.textField.text!.contains("****") {
            SVProgressHUD.showError("请填写正确手机号码")
            return
        }
        
        if csrq.textField.text!.lgf_IsBlank() {
            SVProgressHUD.showError("请选择出生日期")
            return
        }
//        if dq.textField.text!.lgf_IsBlank() {
//            SVProgressHUD.showError("请选择地区")
//            return
//        }
        
//        if !sfz.textField.text!.lgf_IsBlank() && !sfz.textField.text!.lgf_NSPredicate("(^\\d{15}$)|(^\\d{17}([0-9]|X|x)$)") {
//            SVProgressHUD.showError("请填写正确身份证号码")
//            return
//        }
        
        let paramet: [String: String] = [
            "avatar" : tx.lgf_Name ?? "",
            "name" : xm.textField.text ?? "",
            "guardian" : jhr.textField.text ?? "",
            "phone" : sjh.textField.text ?? "",
            "idCard" : sfz.textField.text ?? "",
            "birthday" : (csrq.text + " 00:00:00"),
            "gender" : xb.lgf_Name ?? "",
            "address" : dq.textField.text ?? "",
            "detailAddress" : xxdz.textField.text ?? "",
            "hospitalName": EWEUser.lgf.hospitalName,
            "hospitalId": EWEUser.lgf.hospitalId,
            "lastTime" : Date().lgf_TimeStampString()
        ]
        EWERequest.lgf("patient", paramet, .post, JSONEncoding.default) { [weak self] (data) in
            SVProgressHUD.showSuccess("提交成功")
            Timer.lgf_After(S: 0.5) {
                NotificationCenter.default.post(name: NSNotification.Name(rawValue: "ReloadHzVC"), object: nil)
                self?.navigationController?.popViewController(animated: true)
            }
        } _: { (error) in
            SVProgressHUD.showError(error)
        }
    }

}
